<?php
namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;
use think\Cookie;
use think\Session;

class Statistics extends Controller
{

    public function down()
    {
        if(Request::instance()->isAjax()){
            $user_id = input('post.userid');
            $start_time = input('post.starttime');
            $end_time = input('post.endtime');

            $start_time =  empty($start_time) ? (strtotime(date("Y-m-d",time()))-14*24*3600) : strtotime($start_time);
            $end_time =  empty($end_time) ? strtotime(date("Y-m-d",time())) : strtotime($end_time);
            if($start_time>$end_time){

            }
            $end_time = $end_time+24*3600;

            if(empty($user_id)){
                $res = Db::connect('user_db')->query('select downtime from s_down_a where downtime>='. $start_time .' and downtime<='. $end_time);
            }
            else {
                $res = Db::connect('user_db')->query('select downtime from s_down_a where uid='.$user_id.' and downtime>='. $start_time .' and downtime<='. $end_time);
            }

            if($res){
                foreach($res as $id=>$vo){
                    $temp_time = date('Ymd',$vo["downtime"]);
                    if(!isset($arr[$temp_time])){
                        $arr[$temp_time] = "";
                    }
                    $arr[$temp_time] = $arr[$temp_time]+1;
                }
            }

            $d = array();
            $n = array();
            foreach($arr as $id=>$vo){
                array_push($d, $id);
                array_push($n, $vo);
            }
            $data['d']=$d;
            $data['n']=$n;

            return $data;

        } else {
            return view('down');
        }
    }

    public function browse()
    {
        return view('browse');
    }


}